<!--
 @description 商品分类 （非一级导航入口）
 @fileName Category.vue
 @author zengqiongying
 @created 2022/12/30 11:39:01
-->
<template>
  <div :class="{'mine-container':!isInline}" :style="{'padding-top':`${isInline?0:topHeight}px`}">
    <div v-if="!isInline" ref="top" class="goods-cate-page-head">
      <MineHeader bg-color="" show-statusbar arrow :font-black="false">
        <!-- <div class="head-tabs">
        <div
          v-for="(tab,i) in tabs"
          :key="i"
          :class="['tab',{'active':activeTab===tab.code}]"
          @click="activeTab=tab.code"
        ><span>{{ tab.name }}</span></div>
      </div> -->
        <SearchInput placeholder="搜索" @focus="handleSearch" />
      </MineHeader>
    </div>
    <div class="head-tabs fixed-tabs" :style="{'top':`${isInline?offTop:topHeight}px`,'left':`${isInline?componentTabLeft:0}PX`}">
      <div
        v-for="(tab,i) in tabs"
        :key="i"
        :class="['tab',{'active':activeTab===tab.code}]"
        @click="activeTab=tab.code"
      ><span>{{ tab.name }}</span></div>
    </div>
    <div ref="tabComponent" class="tab-content-box">
      <component
        :is="componentName"
        ref="conComponent"
        :is-inline="isInline"
        :tab-top="componentTabTop"
        :tab-left="componentTabLeft"
      />
    </div>
  </div>
</template>

<script>
import MineHeader from '@/components/MineHeader'
import SearchInput from '@/components/SearchInput'
import StyleSerice from './StyleSerice'
import SpaceCate from './SpaceCate'
export default {
  name: 'GoodsCategory',
  components: { MineHeader, SearchInput, StyleSerice, SpaceCate },
  props: {
    isInline: {
      // 是否内嵌：true 为一级导航内嵌
      type: Boolean,
      default: false
    },
    offTop: {
      // 距离顶部距离
      type: [Number, String],
      default: 0
    }
  },
  data() {
    return {
      tabs: [
        { code: 'spacecate', name: '空间品类' },
        { code: 'styleseries', name: '风格系列' }
      ],
      activeTab: 'spacecate',
      componentTabLeft: 0,
      componentTabTop: 44,
      topHeight: 64
    }
  },

  computed: {
    componentName() {
      return this.activeTab === 'spacecate' ? SpaceCate : StyleSerice
    }
  },

  created() {},
  activated() {
    this.$nextTick(() => {
      this.getTabRect()
    })
  },
  beforeRouteLeave(to, from, next) {
    if (this.$route.name === 'GoodsCategory' && to.name !== 'GoodsList' && to.name !== 'GoodsCategoryChild') {
      this.$removeKeepAliveCache(this)
    }
    next()
  },

  mounted() {
    this.$nextTick(() => {
      this.getTabRect()
    })
  },

  methods: {
    getTabRect() {
      setTimeout(() => {
        if (!this.isInline) {
          this.topHeight = this.$refs.top.offsetHeight
        }
        const rect = this.$refs.tabComponent.getBoundingClientRect()
        this.componentTabTop = rect.top
        this.componentTabLeft = rect.left
      }, 100)
    },
    handleSearch() {
      this.$router.push({
        path: '/search/product'
      })
    }
  }
}

</script>
<style lang='less' scoped>

.goods-cate-page-head{
  position: fixed;
  left: 0;
  top: 0;
  width:100%;
  z-index: 99;
  background: url(~@/assets/my/bg-header.png);
  background-size: 100% auto;
  background-repeat: no-repeat;
  /deep/ .search-input{
    position: relative;
    margin-left: 10px;
    flex: 1;
    .search{
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      margin-left: -20px;
    }
    .search-value{
      background: transparent;
      text-align: center;
      font-size: 14px;
      padding-left: 10px;
    }
  }
}
.head-tabs{
  padding-left: 20px;
  display: flex;
  align-items: center;
  &.fixed-tabs{
    position: fixed;
    z-index: 99;
    left: 90px;
    right: 0;
    background: #fff;
    padding: 16px 0 6px;
    height: 47px;
    box-sizing: border-box;
    justify-content: center;
  }
  .tab{
    padding: 0 12px;
    height: 25px;
    line-height: 25px;
    font-size: 18px;
    color: #999999;
    position: relative;
    &.active{
      color: #333333;
      font-weight: bold;
      span{
        z-index:1;
        position: relative;
      }
      &::before{
        content: '';
        z-index: 0;
        position: absolute;
        left: 50%;
        bottom: 0;
        transform: translateX(-50%);
        width: 72px;
        height: 6px;
        background: linear-gradient(90deg, #00D5C0 0%, #05C4C3 100%);
        border-radius: 3px;
      }
    }
  }
}
.head-tabs&.fixed-tabs~.tab-content-box{
  margin-top: 47px;
}
</style>
